/*
//	Todo: 
		armor detailed loading is funky
		finish thief and witch shop
		create a external editor for item ( for graphical integration later on i.e. world image picker, inv. image picker )

	Bugs:
		None
*/


#ifdef WIN32
#pragma once
#else
#ifndef LABRYRINTH
#define LABRYRINTH
#endif
#endif

#include "main.h"
#include "town.h"

void weaponShop()
{
	ifstream fin;
	int iTemp;
	fin.open("weapon.rpg");

	if (fin.is_open())
	{
		while ( !fin.eof() )
		{
			px.clrscr();
			gString.clear();

			px.text("1. (L)ist inventory");
			px.text("2. (D)etailed inventory");
			px.text("3. E(x)it shop");
			px.getS( gString, "Please select an option: ");

			if( gString == "3" || gString == "x" || gString == "X" )
				break;

			else if( gString == "1" || gString == "l" || gString == "L" )
			{
				for(int i = 1; i < 100; i++)
				{
					if(fin.eof())
						break;

					gString.clear();
					getline (fin,gString,',');
					if(gString.empty()) // nothing left to read
						break;		  // Break where at EOF

					px.number(i,false);
					px.text(". Weapon name: ",false);
					px.text(gString);

					getline( fin,gString,'#'); // go to end of line
					gString.clear();
					
					px.pause("");
				}
			}
			else if( gString == "2" || gString == "d" || gString == "D" )
			{
				for(int i = 1; i < 100; ++i)
				{
					if(fin.eof())
						break;
					gString.clear();
					getline (fin,gString,',');
					if(gString.empty()) // nothing left to read
						break;		  // Break where at EOF

					px.number(i,false);
					px.text(". Weapon name: ",false);
					px.text(gString);
					gString.clear();
					getline (fin,gString,',');
					px.text("Weapon desc: ", false);
					px.text(gString);

					gString.clear();
					getline (fin,gString,',');
					ss.stringToNumber(gString, gInt);
					px.text("Weapon value: ", false);
					px.number(gInt);

					gString.clear();
					getline (fin,gString,',');
					ss.stringToNumber(gString, gInt);
					px.text("Weapon damage dice: ", false);

					gString.clear();
					iTemp=0;
					getline (fin,gString,',');
					ss.stringToNumber(gString,iTemp);
					px.number(iTemp, false); // take number of dice after dice sides so this is not a typo
					px.text("d", false);
					px.number(gInt);

					// clean up
					gInt = 0;

					gString.clear();
					iTemp=0;
					getline (fin,gString,',');
					ss.stringToNumber(gString,iTemp);
					px.text("Weapon critical range: ", false);
					px.number(iTemp);

					gString.clear();
					iTemp=0;
					getline (fin,gString,'#');
					ss.stringToNumber(gString,iTemp);
					px.text("Weapon critical modifier: x", false);
					px.number(iTemp);
					fin >> std::ws; // clear out trailing NL
					
					px.nl(1);
					px.pause("");
				}
			} // else if

			else
				px.text("Please put in a valid choice");

			fin.close();
			px.pause();
		}
	}
}

void armorShop()
{
	ifstream fin;
	int iTemp;
	fin.open("armor.rpg");

	if (fin.is_open())
	{
		while ( !fin.eof() )
		{
			px.clrscr();
			gString.clear();

			px.text("1. (L)ist inventory");
			px.text("2. (D)etailed inventory");
			px.text("3. E(x)it shop");
			px.getS( gString, "Please select an option: ");

			if( gString == "3" || gString == "x" || gString == "X" )
				break;

			else if( gString == "1" || gString == "l" || gString == "L" )
			{
				for(int i = 1; i < 100; i++)
				{
					if(fin.eof())
						break;
					gString.clear();
					getline (fin,gString,',');
					if(gString.empty()) // nothing left to read
						break;

					if(fin.eof())
						break;
					px.number(i,false);
					px.text(". Armor name: ", false);
					px.text(gString);
					getline( fin,gString ); // go to end of line

					px.pause("");
				}
			}
			else if( gString == "2" || gString == "d" || gString == "D" )
			{
				for(int i = 1; i < 100; i++)
				{
					if(fin.eof())
						break;
					gString.clear();
					getline (fin,gString,',');
					if(gString.empty()) // nothing left to read
						break;

					px.number(i,false);
					px.text(". Armor name: ", false);
					px.text(gString);

					gString.clear();
					getline (fin,gString,',');
					px.text("Armor desc: ", false);
					px.text(gString);

					gString.clear();
					getline (fin,gString,',');
					ss.stringToNumber(gString,gInt);
			
					px.text("1 for leather, 2 for studded, 3 for hide, 4 for scale, 5 for bronze,");
					px.text("6 for iron, 7 for splint mail, 8 for half plate, 9 for full plate");
					px.text("Armor type: ", false);
					px.number(gInt,false);

					switch(gInt)
					{
					case 0:
						px.text("\tArmor type by name: Padded");
						break;
					case 1:
						px.text("\tArmor type by name: Leather");
						break;
					case 2:
						px.text("\tArmor type by name: Studded Leather");
						break;
					case 3:
						px.text("\tArmor type by name: Hide");
						break;
					case 4:
						px.text("\tArmor type by name: Scale");
						break;
					case 5:
						px.text("\tArmor type by name: Bronze");
						break;
					case 6:
						px.text("\tArmor type by name: Iron Chain Mail");
						break;
					case 7:
						px.text("\tArmor type by name: Splint Mail");
						break;
					case 8:
						px.text("\tArmor type by name: Half Plate");
						break;
					case 9:
						px.text("\tArmor type by name: Full Plate");
						break;
					}

					gString.clear();
					iTemp=0;
					getline (fin,gString,',');
					ss.stringToNumber(gString,iTemp);
					px.text("Armor value: ", false);
					px.number(iTemp); // value
					
					gString.clear();
					iTemp=0;
					getline (fin,gString,',');
					gString[0] = gString[1];
					gString[1] = ' ';
					ss.stringToNumber(gString,iTemp);
					px.text("Armor protection ( -1 for -1 from damage ): ", false);
					px.number(iTemp);

					gString.clear();
					iTemp=0;
					getline (fin,gString,'#');
					ss.stringToNumber(gString,iTemp);
					px.text("Armor class ( 14 for protect for over 14",false);
					px.text(" 50% hit chance at 14 hit roll ): ",false);
					px.number(iTemp);

					fin >> std::ws; // clear out trailing

					px.nl(1);
					px.pause("");
				}
			} // else if

			else
				px.text("Please put in a valid choice");

			fin.close();
			px.pause();
		}
	}
}

// poison is expensive, works really well on bigger enemys.
// buy poison, special small arms, some pre-poisoned and such
void thiefShop()
{
	// Steal from armor or weaponshop script
}

// sell spells, healing and mana potions, some town warp scrolls which are fairly expensive
// can buy magic rings, but you can only wear one at a time, or they will explode along
// with your hand
void magicShop()
{

}
